<?php
/**
 * @title 449. 序列化和反序列化二叉搜索树
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Codec {
    function __construct() {

    }

    /**
     * @param TreeNode $root
     * @return String
     */
    function serialize($root) {
        /**
         * @since 2023-09-04 存储数值
         */
        $nums = [];

        /**
         * @since 2023-09-04 层次迭代
         */
        $nodes = [$root];
        while($nodes){
            $nextNodes = [];

            /**
             * @since 2023-09-04 遍历当前层点
             */
            foreach ($nodes as $node){
                $nums[] = $node->val;

                if($node->val !== null){
                    $nextNodes[] = $node->left;
                    $nextNodes[] = $node->right;
                } else {}
            }

            /**
             * @since 2023-09-04 下次迭代
             */
            $nodes = $nextNodes;
        }

        /**
         * @since 2023-09-04 去掉尾巴null
         */
        for($i=count($nums)-1; $i>=0; $i--){
            if($nums[$i] === null){
                unset($nums[$i]);
            } else {
                break;
            }
        }

        /**
         * @return
         */
        return json_encode($nums, JSON_UNESCAPED_UNICODE);
    }

    /**
     * @param String $data
     * @return TreeNode
     */
    function deserialize($data) {
        /**
         * @since 2023-09-04 反序列化
         */
        $nums = json_decode($data, true);
        if(empty($nums)){
            return null;
        } else {}

        /**
         * @since 2023-09-04 root
         */
        $tree = new TreeNode($nums[0]);

        /**
         * @since 2023-09-04 赋子节点
         */
        $p = 1;
        $len = count($nums);
        $nodes = [$tree];
        while($nodes AND $p<$len){
            $nextNodes = [];

            /**
             * @since 2023-09-04 左右子树
             */
            foreach($nodes as $node){
                /**
                 * @since 2023-09-04 左子树
                 */
                if($p<$len){
                    if($nums[$p] === null){
                        $leftNode = null;
                    } else {
                        $leftNode = new TreeNode($nums[$p]);
                        $nextNodes[] = $leftNode;
                    }
                    $node->left = $leftNode;
                    $p++;
                } else {
                    break;
                }

                /**
                 * @since 2023-09-04 左子树
                 */
                if($p<$len){
                    if($nums[$p] === null){
                        $rightNode = null;
                    } else {
                        $rightNode = new TreeNode($nums[$p]);
                        $nextNodes[] = $rightNode;
                    }
                    $node->right = $rightNode;
                    $p++;
                } else {
                    break;
                }
            }

            $nodes = $nextNodes;
        }

        /**
         * @return
         */
        return $tree;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202309/2023.09.04.php
 */
$datas = [
    [[2, 1, 3]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
